<template>
    <div id="result">
        <topComponent></topComponent>
        <el-container v-loading="loading_mark">
            <el-header >
                <h2>本场实验结束！感谢您参与本场实验</h2>
                <h2 v-if="! finish">您还需进行<span style="color: #409EFF;">{{least_sub_experiment}}</span>轮实验</h2>
            </el-header>
            <el-main>
                <el-card class="box-card" style="margin-top: 5%;margin-bottom: 5%;">
                    <el-row>
                        <el-col :span="8">
                            <el-statistic  title="您最终的个人资产组合市值">
                                <template slot="formatter">
                                    <div style="margin-bottom: 10px;color: #409EFF;">
                                        {{parseFloat(result.calculated_asset).toFixed(8)}}
                                    </div>
                                </template>
                            </el-statistic>
                        </el-col>
                        <el-col :span="8">
                            <el-statistic  title="累计盈亏">
                                <template slot="formatter">
                                    <div style="margin-bottom: 10px;color: #409EFF;">
                                        {{parseFloat(result.accumulated_profit).toFixed(8)}}
                                    </div>
                                </template>
                            </el-statistic>
                        </el-col>
                        <el-col :span="8">
                            <el-statistic  title="兑换后的累计盈亏（元）">
                                <template slot="formatter">
                                    <div style="margin-bottom: 10px;color: #409EFF;">
                                        {{parseFloat(result.accumulated_profit * result.exchange_rate).toFixed(8)}}
                                    </div>
                                </template>
                            </el-statistic>
                        </el-col>
                    </el-row>
                </el-card>
                <el-button type="primary" v-if="! finish" @click="next_experiment()" >进行下一场实验</el-button>
                <bottomComponent></bottomComponent>
            </el-main>
            
        </el-container>
    </div>
  </template>
  
<script>
import topComponent from '../components/top-component.vue'
import bottomComponent from '../components/bottom-component.vue'
  export default {
    name: 'result',
    components:{
        topComponent,
        bottomComponent
    },
    data(){
        return{
            result:null,
            finish:false,
            least_sub_experiment:null,
            loading_mark:false
        }
    },
    created(){
        this.$global_data.sub_experiment_count --
        this.result = this.$global_data
        if(this.result.sub_experiment_count ==0)
            this.finish = true
        this.least_sub_experiment = this.result.sub_experiment_count
        this.$global_data.sub_experiment.push(JSON.parse(JSON.stringify({
            "calculatedAsset":this.result.calculated_asset,
            "accumulatedProfit":this.result.accumulated_profit,
            "funds":this.result.fund
        })))
        if(this.finish){
            this.loading_mark = true
            for(let i=0;i<this.$global_data.sub_experiment.length;i++){
                this.$global_data.sub_experiment[i].id = null
                this.$global_data.sub_experiment[i].experimentId = null
                this.$global_data.sub_experiment[i].index = null
            }
            let param = {
                "experimentId":this.$global_data.experiment_id,
                "testeeId":this.$global_data.testee_id,
                "subExperiment":this.$global_data.sub_experiment
            }
            this.$axios.post('/test/saveExperimentResult',param).then((res)=>{
                if(res.data.code == 0){
                    this.loading_mark = false
                    const h = this.$createElement;
                    this.$message({
                            message: h('p', null, [
                                h('span', null, '实验全部结束，感谢您的参与'),
                            ])
                    });
                    this.$global_data = null
                    let that = this
                    setTimeout(()=>{that.$router.push('/')},3000)
                    
                }
            })
        }
    },
    methods:{
        next_experiment(){
            this.$global_data.accumulated_profit = 0
            this.$global_data.calculated_asset =  JSON.parse(JSON.stringify(this.$global_data.init_asset)) 
            this.$global_data.current_profit = [0]
            this.$global_data.decision_making_index = 1
            for(let i=0;i<this.$global_data.fund.length;i++){
                this.$global_data.fund[i].allocationRate = [0]
                this.$global_data.fund[i].price = [JSON.parse(JSON.stringify(this.$global_data.fund[i].price[0]))]
            }
            this.$forceUpdate()
            this.$router.push({
            path:'/platform'
        })
        }
    }
  }
</script>
<style>
</style>